import 'package:adb_tool/base/base_view_model.dart';
import 'package:adb_tool/common/app_color.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../widgets/loading_overlay.dart';

abstract class BasePageState<T extends StatefulWidget, VM extends BaseViewModel> extends State<T> {
  late VM viewModel;
  @override
  initState() {
    viewModel = createViewModel();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: viewModel,
      builder: (context, child) {
        return Scaffold(
          backgroundColor: AppColors.white,
          body: Selector<VM, bool>(
            selector: (context, value) => viewModel.isLoading,
            child: buildContentView(context),
            builder: (context, value, child) {
              var loadingText = context
                  .read<VM>()
                  .loadingText;
              return LoadingOverlay(
                isLoading: value,
                loadingText: loadingText,
                child: child ?? Container(),
              );
            },
          ),
        );
      },
    );
  }

  /// 提供内容视图
  buildContentView(BuildContext context);

  VM createViewModel();
}